package com.gxnzd.communityelderly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.dao.DrugContact;
import com.gxnzd.communityelderly.dao.DrugLibraryList;
import com.gxnzd.communityelderly.dao.DrugLibraryInfo;
import com.gxnzd.communityelderly.entity.Community;
import com.gxnzd.communityelderly.entity.DrugLibrary;
import com.gxnzd.communityelderly.entity.Drugs;
import com.gxnzd.communityelderly.entity.Prescribe;
import com.gxnzd.communityelderly.mapper.CommunityMapper;
import com.gxnzd.communityelderly.mapper.DrugsMapper;
import com.gxnzd.communityelderly.service.DrugLibraryService;
import com.gxnzd.communityelderly.mapper.DrugLibraryMapper;
import com.gxnzd.communityelderly.service.DrugsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 23234
* @description 针对表【drug_library】的数据库操作Service实现
* @createDate 2024-10-22 20:44:25
*/
@Service
public class DrugLibraryServiceImpl extends ServiceImpl<DrugLibraryMapper, DrugLibrary>  implements DrugLibraryService{

    @Resource
    private DrugLibraryMapper drugLibraryMapper;

    @Resource
    private CommunityMapper communityMapper;

    @Resource
    private DrugsMapper drugsMapper;

    @Resource
    private DrugsService drugsService;

    /**
     * 药品联系列表
     * @param page
     * @param pageSize
     * @param drugsId
     * @param communityId
     * @return
     */
    @Override
    public R getLibraryList(Integer page, Integer pageSize, Long drugsId, Long communityId) {
        Page<DrugLibrary> list = new Page<>(page,pageSize);

        //拿到药品联系表的数据
        IPage<DrugLibrary> drugList = drugLibraryMapper.selectDrugLiberList(communityId, drugsId,list);

        //在查询的时候将库存为0的药品进行删除（这一步不会影响到此方法最后的返回 只是因为查询操作多 更新频繁）
        LambdaQueryWrapper<DrugLibrary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DrugLibrary::getStock,0);
        drugLibraryMapper.delete(wrapper);

        //药品联系分页
        Page<DrugContact> contactPage = new Page<>();
        List<DrugContact> contacts = new ArrayList<>();

        //循环已经拿到的联系表数据
        for (DrugLibrary drugLibrary : drugList.getRecords()){
            //拿到每项所需要的药品id跟社区id
            Integer itemDrugsId = drugLibrary.getDrugsId();
            Integer itemCommunityId = drugLibrary.getCommunityId();
            //分别去查
            Community community = communityMapper.selectById(itemCommunityId);
            Drugs drugs = drugsMapper.selectById(itemDrugsId);

            //创建dao层的类 并 将数据写进去
            DrugContact drugContact = new DrugContact();
            drugContact.setDrugs(drugs);
            drugContact.setCommunity(community);
            drugContact.setDrugLibrary(drugLibrary);
            //将数据填写进数组里
            contacts.add(drugContact);
        }
        //对新的分页器进行填充
        contactPage.setRecords(contacts);
        contactPage.setTotal(drugList.getTotal());
        contactPage.setPages(drugList.getPages());
        contactPage.setSize(drugList.getSize());
        contactPage.setCurrent(drugList.getCurrent());
        return R.success(contactPage);
    }


    /**
     * 药品入库
     * @param drugLibraryList
     * @return
     */
    @Override
    public R bePutInStorage(List<DrugLibrary> drugLibraryList) {
        if(drugLibraryList.size() ==0){
            return R.error("请传入正确的数据!");
        }
        for(DrugLibrary drugLibrary:drugLibraryList){
            Integer drugsId = drugLibrary.getDrugsId();
            Integer communityId = drugLibrary.getCommunityId();
            //查询以上两个值有没有
            Drugs byDrugsId = drugsMapper.selectById(drugsId);
            if(byDrugsId == null){
                return R.error("ID为"+drugsId+"在药品表中没有数据");
            }
            Community community = communityMapper.selectById(communityId);
            if(community == null){
                return R.error("ID为"+communityId+"的社区不存在");
            }
            //对数据进行添加或者修改
            if(drugLibrary.getStock()<=0){
                return R.error("入库的药品数量不能小于0");
            }
            LambdaQueryWrapper<DrugLibrary> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DrugLibrary::getDrugsId,drugsId)
                    .eq(DrugLibrary::getCommunityId,communityId);
            //如果社区里有了新传入的药品就进行库存++的操作 没有则进行新创建
            DrugLibrary selectOne = drugLibraryMapper.selectOne(wrapper);
            if(selectOne != null){
                selectOne.setStock(selectOne.getStock()+drugLibrary.getStock());
                drugLibraryMapper.update(selectOne,wrapper);
            }else{
                drugLibraryMapper.insert(drugLibrary);
            }
        }
        return R.success("入库成功！");
    }

    /**
     * 药品使用
     * @param reqDrugInfoList
     * @param communityId
     * @return
     */
    @Override
    public R setDrugLibrary(List<Prescribe> reqDrugInfoList, Integer communityId){
        if(reqDrugInfoList.size() ==0){
            return R.error("请传入正确的数据!");
        }
        for(Prescribe drugInfo:reqDrugInfoList){
            Integer drugsId = drugInfo.getDrugsId();
            //查询以上两个值有没有
            Drugs drugs = drugsMapper.selectById(drugsId);
            if(drugs == null){
                return R.error("ID为"+drugsId+"在药品表中没有数据");
            }

            Community community = communityMapper.selectById(communityId);
            if(community == null){
                return R.error("ID为"+communityId+"的社区不存在");
            }
            //对传入的库存进行判断
            if(drugInfo.getDrugNumber()<=0){
                return R.error("消耗的库存数量不能小于0");
            }
            //如果两个数据都有查出这条数据删除库存
            LambdaQueryWrapper<DrugLibrary> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DrugLibrary::getDrugsId,drugsId)
                    .eq(DrugLibrary::getCommunityId,communityId);
            DrugLibrary selectOne = drugLibraryMapper.selectOne(wrapper);
            if (selectOne == null) return R.error("传入的药品ID或社区ID不存在与药品库中！");
            if(selectOne.getStock()>drugInfo.getDrugNumber()){
                //正常情况的使用减少库存
                selectOne.setStock(selectOne.getStock()-drugInfo.getDrugNumber());
                drugLibraryMapper.update(selectOne,wrapper);
            }else if(selectOne.getStock() == drugInfo.getDrugNumber()){
                //正好用完药品库中的药品 对它进行删除
                drugLibraryMapper.deleteById(selectOne);
            }else{
                //传过来的库存数量大于了药品中的数量 消耗失败
                return R.error("此药品在库存中没有"+drugInfo.getDrugNumber()+"数量");
            }
        }
        return R.success("药品库存消耗成功！");
    }

    /**
     * 药品库查询
     * @param drugsName
     * @param communityId
     * @return
     */
    @Override
    public R selectKeyword(String drugsName, Long communityId) {
        List<DrugLibraryList> keyWordList = drugLibraryMapper.selectKeyWordList(drugsName,communityId);
        return R.success(keyWordList);
    }


    /**
     * 修改药品库存
     * @param drugLibrary
     * @return
     */
    @Override
    public R modifyInventory(DrugLibrary drugLibrary) {
        LambdaQueryWrapper<DrugLibrary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DrugLibrary::getCommunityId,drugLibrary.getCommunityId())
                .eq(DrugLibrary::getDrugsId,drugLibrary.getDrugsId());

        //如果传入的库存为0 直接去删
        if(drugLibrary.getStock() == 0){
            return clearInventory(drugLibrary.getDrugsId(),drugLibrary.getCommunityId());
        }
        drugLibraryMapper.update(drugLibrary,wrapper);
        return R.success("修改成功");
    }

    /**
     * 清除库存
     * @param drugsId
     * @param communityId
     * @return
     */
    @Override
    public R clearInventory(Integer drugsId, Integer communityId) {
        LambdaQueryWrapper<DrugLibrary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DrugLibrary::getCommunityId,communityId)
                .eq(DrugLibrary::getDrugsId,drugsId);
        drugLibraryMapper.delete(wrapper);
        return R.success("删除成功！");
    }

    /**
     * 获取某个药品库的药品信息
     * @param communityId
     * @param drugsId
     * @return
     */
    @Override
    public R getDugrLibraryDInfo(Long communityId, Long drugsId) {
        LambdaQueryWrapper<DrugLibrary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DrugLibrary::getCommunityId,communityId)
                .eq(DrugLibrary::getDrugsId,drugsId);
        DrugLibrary drugLibrary = drugLibraryMapper.selectOne(wrapper);
        //获取药品信息
        LambdaQueryWrapper<Drugs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Drugs::getDrugsId,drugsId);
        Drugs drugs = drugsMapper.selectOne(queryWrapper);

        DrugLibraryInfo info = new DrugLibraryInfo();
        info.setDrugs(drugs);
        info.setDrugLibrary(drugLibrary);

        return R.success(info);
    }
}




